package com.dhcc.DRGsHosp.infra.util.license;

import java.io.File;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @title :      License.java
 * @description :
 * @author :      WangTeng
 * @date :2015年3月3日
 * @version :1.0
 * @copyright :DHCC 2013-2014
 */
public class License {
	private Map<String, String> licenseInfo = null;

	private static SimpleDateFormat sdf = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	// private static String LICENSEFULLPATHSTRING =
	// License.class.getResource("/").getPath() + File.separatorChar +
	// "drgs_window.lic";

	public License(String licencePath) {
		this.licenseInfo = getLicence(licencePath);
	}

	public static void createLicence(Map<String, String> licInfo, String path) {
		AESFileReadWrite aes = new AESFileReadWrite(path + File.separatorChar
				+ "drgs_window.lic");
		Map<String, String> licMap = new HashMap<String, String>();
		licMap.put("LICENSE_VER", licInfo.get("LICENSE_VER"));
		licMap.put("LICENSE_RELEASE", licInfo.get("LICENSE_RELEASE"));
		licMap.put("LICENSE_USE_DAY", licInfo.get("LICENSE_USE_DAY"));
		licMap.put("LICENSE_INVALID", licInfo.get("LICENSE_INVALID"));
		licMap.put("LICENSE_START_DATE", licInfo.get("LICENSE_START_DATE"));
		licMap.put("LICENSE_END_DATE", licInfo.get("LICENSE_END_DATE"));
		licMap.put("LICENSE_OWNER", licInfo.get("LICENSE_OWNER"));
		licMap.put("LICENSE_MAC", licInfo.get("LICENSE_MAC"));
		aes.write(licMap);
		System.out.println("License创建成功!");
	}

	public static Map<String, String> getLicence(String path) {
		AESFileReadWrite aes = new AESFileReadWrite(path + File.separatorChar
				+ "drgs_window.lic");
		Map<String, String> map = aes.read2Map();
		return map;
	}

	public String getLicenceVer() {
		return (String) this.licenseInfo.get("LICENSE_VER");
	}

	public String getLicenceOwner() {
		return (String) this.licenseInfo.get("LICENSE_OWNER");
	}

	public String getLicenceMac() {
		return (String) this.licenseInfo.get("LICENSE_MAC");
	}

	public Long getLicenceFullTime() {
		String licence_use_day = (String) this.licenseInfo
				.get("LICENSE_USE_DAY");
		Long fullTime = Long.valueOf(Long.parseLong(licence_use_day) * 24L
				* 60L * 60L * 1000L);
		return fullTime;
	}

	public Date getLicenceInvalidDate() {
		String licence_start_date = (String) this.licenseInfo
				.get("LICENSE_START_DATE");
		String licence_use_day = (String) this.licenseInfo
				.get("LICENSE_USE_DAY");

		Long long_licence_use_day = Long.valueOf(Long
				.parseLong(licence_use_day) * 24L * 60L * 60L * 1000L);
		Date _licence_start_date = null;
		try {
			_licence_start_date = sdf.parse(licence_start_date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Date _invalid_date = new Date(_licence_start_date.getTime()
				+ long_licence_use_day.longValue());
		return _invalid_date;
	}

	public Long getLicenceCanUseTime() {
		Long useFullTime = getLicenceFullTime();

		String licence_start_date = (String) this.licenseInfo
				.get("LICENSE_START_DATE");

		Long usedTime = Long.valueOf(0L);
		try {
			usedTime = Long.valueOf(new Date().getTime()
					- sdf.parse(licence_start_date).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Long canUseTime = Long.valueOf(useFullTime.longValue()
				- usedTime.longValue());
		if (canUseTime.longValue() <= 0L) {
			canUseTime = Long.valueOf(0L);
		}
		return canUseTime;
	}

	public Map<String, String> getLicenceInfo() {
		return this.licenseInfo;
	}

	// public void setLicenceInfo(Map<String, String> licenseInfo) {
	// this.licenseInfo = licenseInfo;
	// }

	public Date getStartDate() {
		String license_start_date = (String) this.licenseInfo
				.get("LICENSE_START_DATE");

		Date _license_start_date = null;
		try {
			_license_start_date = sdf.parse(license_start_date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return _license_start_date;
	}

	public Date getEndDate() {
		String license_end_date = (String) this.licenseInfo
				.get("LICENSE_END_DATE");

		Date _license_end_date = null;
		try {
			_license_end_date = sdf.parse(license_end_date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return _license_end_date;
	}
	
	/**
	 * @methodname    :isMacMatch
	 * @description   :MAC地址是否匹配
	 * @param         :
	 * @author        :WangTeng
	 * @return        :
	 * @date          :2015年3月4日
	 */
	public boolean isMacMatch() {
		String licenseMac = (String)licenseInfo.get("LICENSE_MAC");
		if (licenseMac==null || "null".equals(licenseMac)) {
			return true;
		}else if (licenseMac.equals(getMacAddr())) {
			return true;
		}else {
			return false;
		}
	}
	
	/**
	 * @methodname    :getMacAddr
	 * @description   : 
	 * @param         :
	 * @author        :WangTeng
	 * @return        :
	 * @date          :2015年3月3日
	 */
	private static String getMacAddr(){
		StringBuilder macAddr=new StringBuilder();
		try {
			NetworkInterface networkInterface=NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
			byte[] macAddrByte=networkInterface.getHardwareAddress();
			for (byte b : macAddrByte) {
				String data=Integer.toHexString(b&0xff).toUpperCase();
				if(data.length()==1){
					data="0"+data;
				}
				macAddr.append(data);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return macAddr.toString();
	}

}